package com.spmsys.util;

import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;

/**
 * Servlet工具类
 */
public class ServletUtil {
    /**
     * 获取请求实体方法
     *
     * @param req 请求对象
     * @return 请求数据
     */
    public static JSONObject getRequestBody(@NotNull HttpServletRequest req) {
        StringBuilder stringBuilder = new StringBuilder();
        //如果原型为ContentCachingRequestWrapper则以getContentAsByteArray读取数据
        if (req instanceof ContentCachingRequestWrapper) {
            stringBuilder.append(new String(((ContentCachingRequestWrapper) req).getContentAsByteArray()));
        }
        //如果无法读取数据则以getInputStream读取数据
        if (JSONObject.parseObject(stringBuilder.toString()) == null) {
            try (ServletInputStream servletInputStream = req.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(servletInputStream, StandardCharsets.UTF_8); BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        //返回数据
        return JSONObject.parseObject(stringBuilder.toString());
    }

    /**
     * 获取响应实体方法
     *
     * @param resp 响应对象
     * @return 响应数据
     */
    public static JSONObject getResponseBody(HttpServletResponse resp) {
        //如果原型不是ContentCachingResponseWrapper则终止
        if (!(resp instanceof ContentCachingResponseWrapper)) {
            throw new RuntimeException("原型必须为：ContentCachingResponseWrapper");
        }
        //返回数据
        return JSONObject.parseObject(new String(((ContentCachingResponseWrapper) resp).getContentAsByteArray()));
    }

    /**
     * 获取请求头方法
     *
     * @param req 请求对象
     * @return 请求头数据
     */
    public static @NotNull JSONObject getRequestHeaders(@NotNull HttpServletRequest req) {
        JSONObject headers = new JSONObject();
        //获取请求头
        Enumeration<String> enumeration = req.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();
            String value = req.getHeader(name);
            headers.put(name, value);
        }
        //返回数据
        return headers;
    }

    /**
     * 获取响应头方法
     *
     * @param resp 响应对象
     * @return 响应头数据
     */
    public static @NotNull JSONObject getResponseHeaders(@NotNull HttpServletResponse resp) {
        JSONObject headers = new JSONObject();
        //获取响应头
        for (String name : resp.getHeaderNames()) {
            String value = resp.getHeader(name);
            headers.put(name, value);
        }
        //返回数据
        return headers;
    }
}
